//
//  GlobalConfig.m
//  IPLibTest
//
//  Created by hcm-sd-imac5 on 4/17/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "GlobalConfig.h"
#import "TotalConfig.h"
#import "DeviceDetection.h"
#import "Define.h"
static BOOL isIphone= TRUE ;/// =No Is Ipad /// in case we make Ipad first 
static BOOL isRentina=FALSE;
static BOOL isUnKnowDevice=FALSE; /// if is TRUE it the message will appear.

static BOOL disableClickTabBar=FALSE;
static NSString * strLink=nil;
static NSString * _strKey=nil;
static NSString * _strName=nil;
static int isLandScape=-1;
@implementation GlobalConfig
+(UIImage*) drawText:(NSString*) text 
             inImage:(UIImage*)  image 
             atPoint:(CGPoint)   point 
{
 
    
    UIFont *font = [UIFont boldSystemFontOfSize:18];
    UIGraphicsBeginImageContext(image.size);
   // NSLog(@"imgae size %8.3f %8.3f",image.size.width,image.size.height);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
    [[UIColor whiteColor] set];
    [text drawInRect:CGRectIntegral(rect) withFont:font]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // NSLog(@"newImage size %8.3f %8.3f",newImage.size.width,newImage.size.height);
    UIGraphicsEndImageContext();
    
    return newImage;
}
-(void)dealloc
{
    [super dealloc];
    SAFE_RELEASE(strLink);
    SAFE_RELEASE(_strKey);
    SAFE_RELEASE(_strName);
    isIphone= TRUE ;
    isRentina=FALSE;
    isUnKnowDevice=FALSE;
    disableClickTabBar=FALSE;
}
+(void)setOrient:(int)bValue
{
   
    isLandScape=bValue;
    
}
+(int)getAddLandScapeFor:(int)iN
{
    if(iN==0)
    {
        /// scrollView
        if(IS_IPHONE5)
        {
            return -90;
        }
        else{
            return -90;
        }
    }
    return 0;
}
+(int)getAddPortraitFor:(int)iN
{
    if(iN==0)
    {
        /// scrollView
        if(IS_IPHONE5)
        {
            return 280;
        }
        else{
            return 80;
        }
    }
    return 0;
}
+(BOOL)getOrient
{
    return isLandScape;
}
+(void)setLink:(NSString *)str
{
    SAFE_RELEASE(strLink);    
    strLink=[[NSString stringWithFormat:@"%@",str] retain];
    strLink = [strLink stringByReplacingOccurrencesOfString:@" " withString:@""];
       
}
+(NSString *)getLink
{
    return strLink;
}
+(void)setkey:(NSString*)strKey
{
    SAFE_RELEASE(_strKey); 
    _strKey=[[NSString stringWithFormat:@"%@",strKey] retain];
    _strKey = [strLink stringByReplacingOccurrencesOfString:@" " withString:@""];
    
}
+(NSString*)getKey
{
    return _strKey;
}
+(void)setName:(NSString*)strName
{
    SAFE_RELEASE(_strName); 
    _strName=[[NSString stringWithFormat:@"%@",strName] retain];
    _strName = [strLink stringByReplacingOccurrencesOfString:@" " withString:@""];
}
+(NSString*)getName
{
    return _strName;
}
+(CGRect)getRectIndicator:(UIInterfaceOrientation)orie andIsGetCurrentOrient:(BOOL)isYES
{
    CGRect rect;
    if(isYES==TRUE)
    {
    
        //UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
        //BOOL isCurrentlyLandscapeView = UIDeviceOrientationIsLandscape(deviceOrientation);
        int result=[GlobalConfig getOrient];
        BOOL isCurrentlyLandscapeView=FALSE;
        if(result==-1)
        {
            UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
            isCurrentlyLandscapeView=UIDeviceOrientationIsLandscape(deviceOrientation);
        }
        else if(result==0)// landscape
        {
            isCurrentlyLandscapeView=TRUE;
        }
        else if(result==1)// portrait
        {
            isCurrentlyLandscapeView=FALSE;
        }    

        if(IS_IPHONE5==TRUE)
        {
            if(isCurrentlyLandscapeView==TRUE)
            {
                rect=CGRectMake(0,0, 568, 320);
            }
            else
            {
                rect=CGRectMake(0, 0, 320, 568);
            }
        }
        else
        {
            if(isCurrentlyLandscapeView==TRUE)
            {
                 rect=CGRectMake(0,0, 480, 320);
            }
            else
            {
                rect=CGRectMake(0, 0, 320, 480);
            }
        }
        
        
        
        
    }
    else
    {
        switch( orie )
        {
                
            case UIDeviceOrientationPortrait:
            case UIDeviceOrientationPortraitUpsideDown:
            {
                if(IS_IPHONE5==TRUE)
                {
                    rect=CGRectMake(0, 0, 320, 568);
                }
                else
                {
                    rect=CGRectMake(0, 0, 320, 480);
                    
                }
                //75///
            } break;
                
            case UIDeviceOrientationLandscapeLeft:
            case UIDeviceOrientationLandscapeRight:
            {
                if(IS_IPHONE5==TRUE)
                {
                    rect=CGRectMake(0,0, 568, 320);
                }
                else
                {
                     rect=CGRectMake(0,0, 480, 320);
                     
                }
               
            } break;
                
        }
    }
    return rect;
}
+(int)getIndexDot:(NSString*)str
{
    BOOL isHave=TRUE;
    int index=0;
    
    NSString* tempStr=[[NSString stringWithFormat:@"%@",str] retain];
    do {
        ////
        if ([tempStr length] > 0)
        {
            
            NSString* feng = [tempStr substringWithRange:NSMakeRange(index, 1)];
            NSLog(@"str =%@",feng);
            if([feng isEqualToString:@"."]==TRUE)
            {
                index++;
            }
            else
            {
                isHave=FALSE;
                
            }
        }
        
    } while (isHave==TRUE);
    SAFE_RELEASE(tempStr);
    return index;
}
+(int)getFirstDot:(NSString*)strHaveDot
{
    BOOL isMeet=FALSE;
    int index=0;
    NSString* tempStr=[[NSString stringWithFormat:@"%@",strHaveDot] retain];
    
    do {
        NSString* feng = [tempStr substringWithRange:NSMakeRange(index, 1)];
        // NSLog(@"str =%@",feng);
        if([feng isEqualToString:@"."]==TRUE)
        {
            isMeet=TRUE;
        }
        else
        {
            index++;
            
        }
        
    } while (isMeet==FALSE);    
    SAFE_RELEASE(tempStr);
    return index;
    
}
#pragma mark ---------------------------CONVERT POINT ALL DEVICE-----------------------------------------------------
+(CGPoint)CoverPointForAllDevice:(CGPoint)p andCGPointAlpha:(CGPoint)pAlpha
{
    CGPoint temp=p;
    if(isIphone==YES)
    {
        temp=CGPointMake(p.x*XIPhone+pAlpha.x, p.y*YIPhone+pAlpha.y);
    }
    else
    {
        temp=CGPointMake(p.x+pAlpha.x, p.y+pAlpha.y);
    }
    return  temp;
    
}
#pragma mark ---------------------------DETECH DEVICE-----------------------------------------------------
+(void)SettingUpByDetechDevice
{
    int iFlatFromDevice=[DeviceDetection platformbyInt];
    if(iFlatFromDevice==1||iFlatFromDevice==2||iFlatFromDevice==3||iFlatFromDevice==4)
    {
        // Is Iphone 3 no HD
        isIphone=TRUE;
        isRentina=FALSE;
        isUnKnowDevice=FALSE;
    }
    else if(iFlatFromDevice==4||iFlatFromDevice==5||iFlatFromDevice==6)
    {
        // Is Iphone 4 Have HD
        isIphone=TRUE;
        isRentina=TRUE;
        isUnKnowDevice=FALSE;
        
    }
    else if(iFlatFromDevice==7||iFlatFromDevice==8||iFlatFromDevice==9||iFlatFromDevice==10)
    {
        // Is Ipod
        isIphone=TRUE;
        isRentina=FALSE;
        isUnKnowDevice=FALSE;
    }
    else if(iFlatFromDevice==11||iFlatFromDevice==12||iFlatFromDevice==13||iFlatFromDevice==14)
    {
        // Is Ipad 1 vs 2 no HD
        isIphone=NO;
        isRentina=FALSE;
        isUnKnowDevice=FALSE;
    }
    else if(iFlatFromDevice==15||iFlatFromDevice==16)
    {
        // is Simulator
        isIphone=NO;
        isRentina=FALSE;
        isUnKnowDevice=FALSE;
        
    }
    else
    {
        isRentina=FALSE;
        isUnKnowDevice=TRUE;
        NSLog(@"----unKnow device----");
    }
    
}
#pragma mark ---------------------------TabBarEnable DEVICE-----------------------------------------------------
+(void)setTabBarEnable:(BOOL)value
{
    disableClickTabBar=value;
    
}
+(BOOL)getTabBarEnable
{
    return  disableClickTabBar;
}
@end
